import Tank from './tank.js'
import Direction from './direction.js'

export default class EnemyTank extends Tank {
  constructor(x, y, width, height, imgSrc = './assets/enemy-down.png',life = 50,direction = Direction.DOWN,speed = 1) {
    super(x, y, width, height, imgSrc,life,direction,speed)
    this.sprites = ['./assets/enemy-up.png', './assets/enemy-left.png', './assets/enemy-down.png', './assets/enemy-right.png']
    this.autoDirection()
  }

  autoDirection(cols) {
    var randomDir = () => {
      var time = Math.floor(Math.random() * 1500 + 500)
      this.autoTimer = setTimeout(() => {
        var r = Math.random() * 100
        var dir = 0
        if (r < 50) {
          dir = Direction.DOWN
        } else if (r < 70) {
          dir = Direction.LEFT
        } else if (r < 90) {
          dir = Direction.RIGHT
        } else {
          dir = Direction.TOP
        }
        this.direction = dir
        randomDir()
      }, time)
    }

    randomDir()

    // this.autoTimer = setInterval(() => {
    //   var r = Math.random() * 100
    //   var dir = 0
    //   if (r < 50) {
    //     dir = Direction.DOWN
    //   } else if (r < 70) {
    //     dir = Direction.LEFT
    //   } else if (r < 90) {
    //     dir = Direction.RIGHT
    //   } else {
    //     dir = Direction.TOP
    //   }
    //   this.direction = dir
    // }, 2000)
  }

  autoMove(cols) {
    this.move(this.direction, cols)
  }
}
